<?php

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chkGametype = chkGametype($gametype);
$chk = new CheckMode_3d($curruser['acl'], 1, $gametype);
$chk->checkShow3dPage();
$g_betname = judge_type($gametype);
unset($chk);
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
$ui = new UI_3D(true);
$tpl = new Template2();
$conf = new Config();
if ($chkGametype == 3)
{
    include_once( "class_5h/MyLotto_5h.php" );
    include_once( "class_5h/Bet_5h.php" );
    $MyLotto_3d = new MyLotto_5h($gametype);
}
elseif ($chkGametype == 4)
{
    include_once( "class_sb/MyLotto_sb.php" );
    include_once( "class_sb/Bet_sb.php" );
    $MyLotto_3d = new MyLotto_sb($gametype);
}
else
{
    include_once( "class_3d/MyLotto_3d.php" );
    include_once( "class_3d/Bet_3d.php" );
    $MyLotto_3d = new MyLotto_3d($gametype);
}
$id = intval(getvar("id"));
$lottoData = $MyLotto_3d->getDataById($id);
$postTime = $conf->get("paper_post_bet");
$today = date("Hi") <= $postTime ? date("Y-m-d", strtotime("-1 day")) : date("Y-m-d");
$x = $lottoData['open'] == $today ? "" : "x";
if ($chkGametype == 4)
	$Bet_3d = new Bet_sb($gametype, $x);
else
	$Bet_3d = new Bet_3d($gametype, $x);
if ($lottoData == false)
{
    $ui->showErrorPage("{$tblName}错误的期数资料！");
    exit();
}
else if ($lottoData['n1'] < 0 || $lottoData['n2'] < 0 || $lottoData['n3'] < 0)
{
    $ui->showErrorPage("{$tblName}还没有设定号码，无法颁奖！");
    exit();
}
else if (!isset($_GET['mod']) && $conf->get("calcbet_{$tbl}") == 1)
{
    $ui->showErrorPage("{$tblName} 其他操盘者正在颁奖过帐中，同一时间请勿重复颁奖！");
    exit();
}
if ($lottoData['state'] != "2")
{
    exit();
}
$ui->header();
echo "<p>{$tblName} 颁奖结果 期数：" . ($lottoData['round']) . "&#12288;&#12288;<font color=red>（请仔细检查是否过帐正确，再选择重新颁奖或离开）</font></p>";
echo "<div id=\"resultDiv\">";
echo "<table width=758 border=1 cellpadding=3 cellspacing=0>";
echo "<tr class=td1 align=center nowrap><td nowrap>下注种类</td><td nowrap>笔数</td><td nowrap>下注金额</td>";
echo "<td nowrap>会员</td><td nowrap>公司</td><td nowrap>收飞金额</td><td nowrap>收飞结果</td><td nowrap>总监退水</td><td nowrap>公司合计</td>";
$total_count = 0;
$total_amount = 0;
$sum_result1 = 0;
$sum_result4 = 0;
$sum_result5 = 0;
$sum_result6 = 0;
$sum_result7 = 0;
$sum_result8 = 0;
$sum_result9 = 0;
$sum_result0 = 0;
$sum_resultG = 0;
foreach ($g_betname as $key => $val)
{
    $kArr = explode("_", $key);
    $k = $kArr[0];
    $subid = (isset($kArr[1])?$kArr[1]:0);
    $sql = "!!select sum(amount),sum(b1),sum(r5),sum(c5),count(amount),sum(r6),sum(g5) from {$x}bet_{$tbl} where lotto_id={$id} and bettype={$k} and subid={$subid} and rame=9";
	$rs2 = getdb($sql);
    $this_count = $rs2->f[4];
    $amount = 0;
    $result1 = 0;
    $result5 = 0;
    $result4 = 0;
    $result6 = 0;
    $result7 = 0;
    $result8 = 0;
    $result9 = 0;
    $result0 = 0;
    $resultG = 0;
    if (0 < $this_count)
    {
        $amount = $rs2->f[0];
        $result1 = $rs2->f[1];
        $result5 = $rs2->f[2];
        $result4 = 0 - $rs2->f[3];
        $result6 = $rs2->f[5];
        $resultG = $rs2->f[6];
    }
    echo "<tr align=right class=td2>";
    echo "<td width='11%' align=left nowrap>" . $val;
    echo "<td nowrap>" . $this_count;
    echo "<td nowrap>" . $amount;
    $total_count += $this_count;
    $result8 = 0;
    $result9 = 0;
    $sql = "!!select sum(amount*percent255/100),sum(r5),sum(r6),sum(g5) from {$x}bet2_{$tbl} where lotto_id={$id} and bettype={$k} and subid={$subid} and rame=9";
    $rsb = getdb($sql);
    if (0 < $rsb->f[0])
    {
        $result8 = $rsb->f[0];
        $result9 = $rsb->f[1];
        $result6 += $rsb->f[2];
        $resultG += $rsb->f[3];
    }
    if (0 < $sum_result6)
    {
        $result7 = $result6 / $sum_result6 * 100;
    }
    echo "<td nowrap>" . say_numbercolor($result1);
    echo "<td nowrap>" . say_numbercolor($result5);
    echo "<td nowrap>" . say_numbercolor($result8);
    echo "<td nowrap>" . say_numbercolor($result9);
    echo "<td nowrap>" . say_numbercolor($resultG);
    $result0 = $result5 + $result9;
    echo "<td nowrap>" . say_numbercolor($result0 - $resultG);
    $total_amount += $amount;
    $sum_result1 += $result1;
    $sum_result5 += $result5;
    $sum_result7 += $result7;
    $sum_result8 += $result8;
    $sum_result9 += $result9;
    $sum_resultG += $resultG;
    $sum_result0 += $result0 - $resultG;
}
$sum_resultA = $sum_result0;
echo "<tr class=td3 align=right>";
echo "<td colspan=1 align=right><b>总计</b><td nowrap><b>" . $total_count . "</b><td nowrap><b>" . $total_amount . "</b><td nowrap><b>" . say_numbercolor($sum_result1) . "</b><td nowrap><b>" . say_numbercolor($sum_result5) . "</b><td nowrap><b>" . say_numbercolor($sum_result8) . "</b><td nowrap><b>" . say_numbercolor($sum_result9) . "</b><td nowrap><b>" . say_numbercolor($sum_resultG) . "</b><td nowrap><b>" . say_numbercolor($sum_result0) . "</b>";
echo "</td></tr>";
echo "</table>";
echo "<p><font color=red size=4><b>{$tblName}颁奖过帐完毕！&#12288;&#12288;&#12288;</b></font><input type=button value='回上一页' onclick=\"location.href='op.php?op=admin&fp=history_3d&mod=1&calc=1&id={$id}&gametype={$gametype}'\">&#12288;<input type=button value='重新颁奖' id=\"recalc\" ></p><br><br>";
echo "</div>";
echo "<script>\r\n// 颁奖\r\n$(function(){\r\n  $(\"input:button#recalc\").click(function(){\r\n    $(\"div#resultDiv\").empty();\r\n    $(\"div#resultDiv\").addClass(\"td3\").text(\"" . $tblName . " 重新颁奖中，请稍後...\");\r\n    $.get(\"?op=admin&fp=xmlSetConf_3d&cmd=calc&id=" . $id . "&gametype=" . $gametype . "\", function(data){\r\n      if(data == \"true\"){\r\n        $().chkLottoOK(); // 检查是否开奖完成\r\n      }else{\r\n        alert(\"error\");\r\n      }\r\n    });\r\n  });\r\n\r\n  \r\n  // 检查是否颁奖完毕\r\n  $.fn.chkLottoOK = function(){\r\n    $.ajax({\r\n      type: \"POST\",\r\n      url: \"?op=agent&fp=xmlCheckLotto_3d\",\r\n      dataType: \"json\",\r\n      data: \"id=" . $id . "&gametype=" . $gametype . "\",\r\n      success: function(rdata){\r\n        if(rdata.state==\"2\"){\r\n          window.top.mainFrame.location.replace(\"?op=admin&fp=calc_3d&mod=calc&id=" . $id . "&gametype=" . $gametype . "\"); \r\n        }else{\r\n          setTimeout(\"$().chkLottoOK()\",1000);   \r\n        }\r\n      }\r\n    });\r\n    \r\n  \r\n  };\r\n  \r\n});\r\n\r\n</script>\r\n\r\n\r\n";
$ui->footer();